package com.egg.demo.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.egg.common.core.model.controller.WebController;
import com.egg.common.core.model.response.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xerial.snappy.Snappy;

import java.io.IOException;
import java.nio.charset.StandardCharsets;


@Slf4j
@Api(tags = WebController.API_PREFIX + "压缩与解压缩")
@RestController
@RequestMapping("/snappy")
public class WebSnappyController extends WebController {


    @ApiOperation(value = "压缩与解压缩")
    @PostMapping
    public ResponseData<String> compressedPost(@RequestBody JSONObject json) throws IOException {
        // 压缩
        byte[] compressedData = Snappy.compress(json.toJSONString().getBytes(StandardCharsets.UTF_8));
        // 解压
        String decompressedData = Snappy.uncompressString(compressedData, StandardCharsets.UTF_8);
        log.info("\n原文 {} \n压缩 {} \n解压 {} ", json, compressedData, decompressedData);
        return ResponseData.success(decompressedData);
    }


}

